package com.gpscookie.mobile;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;

import de.enough.polish.ui.Form;
import de.enough.polish.ui.Spacer;
import de.enough.polish.ui.StringItem;
import de.enough.polish.ui.TextField;
import de.enough.polish.util.Locale;
import de.enough.polish.ui.Gauge;

public class MainScreen extends BasicScreen implements LocationListener
{
	private TextField txtLat;
	private TextField txtLon;
	private TextField txtTag;
	private Gauge gaugeProcess;
	private StringItem labInfo;
	private Command cmdLocation;
	private Command cmdSearch;
	private Command cmdCreate;
	private Command cmdOption;
	private int count = 0;
	
	public MainScreen(String title, GPSCookie glcl) {
		super(title, glcl);
		//#style textField
		this.txtLat = new TextField(Locale.get("LAT"),GPSCookie.rmsstore.get(RMSStore.LAT, ""),10,TextField.DECIMAL);
		this.append(txtLat);
		//#style textField		
		this.txtLon = new TextField(Locale.get("LON"),GPSCookie.rmsstore.get(RMSStore.LON, ""),10,TextField.DECIMAL);
		this.append(txtLon);
		//#style textFieldBig
		this.txtTag = new TextField(Locale.get("TAG"),GPSCookie.rmsstore.get(RMSStore.TAG, ""),10,TextField.ANY);
		this.append(txtTag);
		//#style infoText
		this.labInfo = new StringItem(Locale.get("STATUS"), Locale.get("READY"));
		this.append(this.labInfo);
		//this.gaugeProcess = new Gauge("", false, Gauge.INDEFINITE, 0);
		//this.append(this.gaugeProcess);
		
		this.cmdLocation = new Command(Locale.get("MYLOC"), Command.SCREEN, 20);
		this.cmdSearch = new Command(Locale.get("SEARCH"), Command.SCREEN, 10);
		this.cmdCreate = new Command(Locale.get("CREATE"), Command.SCREEN, 30);
		this.cmdOption = new Command(Locale.get("SETTING"), Command.SCREEN, 40);		
		this.addCommand(this.cmdOption);
		this.addCommand(this.cmdCreate);
		this.addCommand(this.cmdSearch);
		this.addCommand(this.cmdLocation);
		this.setCommandListener(this);
		
		//#style stringItem
		StringItem cpy = new StringItem("", Locale.get("COPYRIGHT"));
		this.append(new Spacer(this.getWidth(), 30));
		this.append(cpy);
		this.appendAd();
	}

	public void commandAction(Command arg0, Displayable arg1) {		
		if (arg0 == cmdLocation) {		
			//this.gaugeProcess.setValue(Gauge.CONTINUOUS_RUNNING);
			this.labInfo.setText(startGPS());
		} else if (arg0 == cmdSearch) {
			String url = "http://gpscookieapi.appspot.com/search?lat="+txtLat.getString().trim()+"&lon="+txtLon.getString().trim();
			try {
				url+="&tags="+GPSCookie.string2http(txtTag.getString());
			} catch (Exception e) {
				
			}
			this.globalClass.search(url);
			this.globalClass.rmsstore.put(RMSStore.TAG, txtTag.getText());
            this.globalClass.rmsstore.put(RMSStore.LAT, txtLat.getText());
            this.globalClass.rmsstore.put(RMSStore.LON, txtLon.getText());
		} else if (arg0 == cmdOption) {
			this.globalClass.settings();
		} else if (arg0 == cmdCreate) {
			this.globalClass.create();
		}
	}

	protected String startGPS() {		
		try {			
			// Set criteria for selecting a location provider:
			// accurate to 500 meters horizontally
			Criteria cr= new Criteria();
			cr.setHorizontalAccuracy(500);
	
			// Get an instance of the provider
			LocationProvider lp= LocationProvider.getInstance(cr);
	
			if (lp==null) {
				return Locale.get("NOGPS");
			} else {
				lp.reset();
				lp.setLocationListener(this, 10, 7, 9);
				count = 0;
			}
			return Locale.get("WAITMSG");
		} catch (Exception e) {
			return e.getMessage();
		}
	}


	public void locationUpdated(LocationProvider provider, Location location) {
	   QualifiedCoordinates cor = location.getQualifiedCoordinates();
       if(cor == null)
       {
    	   count = (count + 1) % 65535;
    	   String times = String.valueOf(count);
    	   labInfo.setText(Locale.get("GPSRETRY", times));
    	   //this.gaugeProcess.setValue(Gauge.CONTINUOUS_RUNNING);
       } else {
           double lon = location.getQualifiedCoordinates().getLongitude();

           double lat = location.getQualifiedCoordinates().getLatitude();

           txtLon.setString(Double.toString(lon));
           txtLat.setString(Double.toString(lat));
           labInfo.setText(Locale.get("READY"));  
       }		
	}

	public void providerStateChanged(LocationProvider arg0, int newState) {
		if (newState==LocationProvider.AVAILABLE) {
			this.labInfo.setText("...AVAILABLE");
		} else if (newState==LocationProvider.OUT_OF_SERVICE) {
			this.labInfo.setText("...OUT_OF_SERVICE");
		} else if (newState==LocationProvider.TEMPORARILY_UNAVAILABLE) {
			this.labInfo.setText("...TEMPORARILY_UNAVAILABLE");
		} else {
			this.labInfo.setText("..."+newState);
		}		
	}	
}
